<html>
<head><meta charset="utf-8"><title>lost in lifetimes · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html">lost in lifetimes</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221234346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234346">(Dec 30 2020 at 18:28)</a>:</h4>
<p>I've encountered this problem a few times already and every time I get stuck. Maybe someone here knows a solution? In short, I have a struct that carries a mut ref and I'd like a method that accesses its contents, but I get tripped by lifetimes.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">MyRef</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="kt">u64</span><span class="p">);</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">do_a_thing</span><span class="p">(</span><span class="n">foo</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">u64</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">myref</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MyRef</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">foo</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">do_a_thing</span><span class="p">(</span><span class="n">myref</span><span class="p">.</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="c1">// works</span>
<span class="w">    </span><span class="c1">// I want a method that does the same thing, like:</span>
<span class="w">    </span><span class="c1">//do_a_thing(myref.contents());</span>
<span class="w">    </span><span class="nb">drop</span><span class="p">(</span><span class="n">myref</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// I tried to relax lifetimes as much as possible. This works in the case of non-mut references;</span>
<span class="c1">// for mut-refs `drop(myref)` above gives an error.</span>
<span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="n">MyRef</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">contents</span><span class="o">&lt;'</span><span class="na">b</span><span class="p">,</span><span class="w"> </span><span class="o">'</span><span class="na">c</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">b</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="na">c</span> <span class="nc">mut</span><span class="w"> </span><span class="kt">u64</span><span class="w"></span>
<span class="w">         </span><span class="k">where</span><span class="w"> </span><span class="o">'</span><span class="na">b</span>: <span class="o">'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="o">'</span><span class="na">a</span>: <span class="o">'</span><span class="na">c</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221234555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234555">(Dec 30 2020 at 18:31)</a>:</h4>
<p><span class="user-mention" data-user-id="245339">@Nadrieril</span> I think you have the bounds wrong: you want <code>'a: 'b</code> and <code>'b: 'c</code></p>



<a name="221234569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234569">(Dec 30 2020 at 18:31)</a>:</h4>
<p>currently your function says that '&amp;mut self must live at least as long as the entire <code>MyRef</code>'</p>



<a name="221234643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234643">(Dec 30 2020 at 18:32)</a>:</h4>
<p>hm this exact code compiles on the playground</p>



<a name="221234666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234666">(Dec 30 2020 at 18:33)</a>:</h4>
<p>oh, your suggestion works!!</p>



<a name="221234690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234690">(Dec 30 2020 at 18:33)</a>:</h4>
<p>I am so confused by lifetimes x)</p>



<a name="221234696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234696">(Dec 30 2020 at 18:33)</a>:</h4>
<p>thanks a lot!</p>



<a name="221234701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221234701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221234701">(Dec 30 2020 at 18:33)</a>:</h4>
<p>you're welcome :)</p>



<a name="221235041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235041">(Dec 30 2020 at 18:39)</a>:</h4>
<p>huh, after your suggestion I tried to simplify, and with lifetime elision I get the naivest solution possible, which works x)</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">contents</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">u64</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221235153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235153">(Dec 30 2020 at 18:40)</a>:</h4>
<p>yeah, the <code>'b: 'c</code> bound wasn't actually helping you: it meant the lifetime was allowed to live <em>shorter</em> than the borrow of self. But if you have a <code>&amp;'b u64</code> already, you can always shorten the lifetime as much as you like by reborrowing.</p>



<a name="221235181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235181">(Dec 30 2020 at 18:41)</a>:</h4>
<p>and the <code>'a: 'b</code> bound is implicit here because <code>&amp;mut self</code> gets a new fresh lifetime, not the 'a from the type</p>



<a name="221235229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235229">(Dec 30 2020 at 18:41)</a>:</h4>
<p>rust makes it way too easy to write functions that restrict the lifetime unnecessarily <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="221235282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235282">(Dec 30 2020 at 18:42)</a>:</h4>
<p>speaking of which, <a href="https://github.com/raphlinus/pulldown-cmark/issues/509">https://github.com/raphlinus/pulldown-cmark/issues/509</a> was an <em>adventure</em></p>



<a name="221235325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235325">(Dec 30 2020 at 18:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/lost.20in.20lifetimes/near/221235181">said</a>:</p>
<blockquote>
<p>and the <code>'a: 'b</code> bound is implicit here because <code>&amp;mut self</code> gets a new fresh lifetime, not the 'a from the type</p>
</blockquote>
<p>Yeah, I've stopped trusting implicit outlives relations from things like <code>&amp;'a Foo&lt;'b&gt;</code> because occasionally some <code>'tcx</code> doesn't get such a bound and I get confused</p>



<a name="221235461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235461">(Dec 30 2020 at 18:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/lost.20in.20lifetimes/near/221235282">said</a>:</p>
<blockquote>
<p>speaking of which, <a href="https://github.com/raphlinus/pulldown-cmark/issues/509">https://github.com/raphlinus/pulldown-cmark/issues/509</a> was an <em>adventure</em></p>
</blockquote>
<p>wow, that's intense x)</p>



<a name="221235498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235498">(Dec 30 2020 at 18:45)</a>:</h4>
<p>yeah I spent most of the day on it lol</p>



<a name="221235517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235517">(Dec 30 2020 at 18:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/lost.20in.20lifetimes/near/221235153">said</a>:</p>
<blockquote>
<p>yeah, the <code>'b: 'c</code> bound wasn't actually helping you: it meant the lifetime was allowed to live <em>shorter</em> than the borrow of self. But if you have a <code>&amp;'b u64</code> already, you can always shorten the lifetime as much as you like by reborrowing.</p>
</blockquote>
<p>I was under the impression that maybe <code>&amp;mut</code> references got less magically reborrowed than simple <code>&amp;</code> refs</p>



<a name="221235582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235582">(Dec 30 2020 at 18:46)</a>:</h4>
<p>&amp;mut is not <code>Copy</code>, but I think it gets reborrowed in all the cases that matter</p>



<a name="221235591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235591">(Dec 30 2020 at 18:46)</a>:</h4>
<p>what tripped me with the OP is that for plain <code>&amp;</code> my choice of lifetime bounds worked</p>



<a name="221235592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235592">(Dec 30 2020 at 18:46)</a>:</h4>
<p>I don't remember when the difference matters off the top of my head</p>



<a name="221235626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221235626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221235626">(Dec 30 2020 at 18:46)</a>:</h4>
<p>granted my choice of bounds was purely tetris x)</p>



<a name="221243828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221243828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221243828">(Dec 30 2020 at 20:28)</a>:</h4>
<p>I repeat my assertion that explicit outlives bounds are always ("99.999%") either (a) unneeded (b) wrong</p>



<a name="221244638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221244638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221244638">(Dec 30 2020 at 20:38)</a>:</h4>
<p>I'd love it if the compiler could tell me "you could relax this bound, it's too restrictive"</p>



<a name="221245164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221245164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221245164">(Dec 30 2020 at 20:46)</a>:</h4>
<p>Would be nice, but I'd suppose it's hard and generally "not wanted", for much the same reason that top-level inference doesn't exist.</p>



<a name="221245279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221245279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221245279">(Dec 30 2020 at 20:48)</a>:</h4>
<p>Easier shown with types:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>WARNING: types `i32` could be made more general:

 fn add&lt;T: Add&lt;Output = T&gt;&gt;(a: T, b: T) -&gt; T { a + b }
</code></pre></div>



<a name="221245305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221245305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221245305">(Dec 30 2020 at 20:48)</a>:</h4>
<p>I guess that's one step further than what you propose though.</p>



<a name="221245338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221245338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221245338">(Dec 30 2020 at 20:49)</a>:</h4>
<p>More like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>WARNING: bound `Clone` is not used in this function
</code></pre></div>



<a name="221245424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221245424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221245424">(Dec 30 2020 at 20:50)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust-clippy/issues/5719">https://github.com/rust-lang/rust-clippy/issues/5719</a></p>



<a name="221245460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221245460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221245460">(Dec 30 2020 at 20:50)</a>:</h4>
<p>not sure about the lifetime variants though</p>



<a name="221246472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221246472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221246472">(Dec 30 2020 at 21:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/lost.20in.20lifetimes/near/221245338">said</a>:</p>
<blockquote>
<p>More like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p><div class="codehilite"><pre><span></span><code>WARNING: bound `Clone` is not used in this function
</code></pre></div><br>
</p>
</blockquote>
<p>Huh, I'd find that one quite useful</p>



<a name="221254590"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221254590" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221254590">(Dec 30 2020 at 23:12)</a>:</h4>
<p><span class="user-mention" data-user-id="245339">@Nadrieril</span>, regarding <code>&amp;</code> _vs._ <code>&amp;mut</code> with lifetimes: since <code>&amp;'long _</code> is <code>Copy</code>, one can go from <code>&amp;'short &amp;'long T</code> to <code>&amp;'long T</code> simply by dereferencing (also, by deref coercion, this can even happen implicitly).<br>
For a potentially non-<code>Copy</code> inner reference type (such as <code>&amp;'long mut _</code>), this is not possible, and the only "smooth" / easy way to flatten indirection is by reborrowing, so that <code>&amp;'short mut &amp;'long mut T</code> becomes <code>&amp;'short mut T</code> (note that the <code>Copy</code> counterpart of <code>&amp;mut</code> is <code>mem::replace</code>)</p>
<p>So with <code>&amp;mut</code>s one has to "get the nested lifetimes right", whereas with <code>&amp;</code>s one often has the luxury of getting away with not doing it.</p>



<a name="221254844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221254844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221254844">(Dec 30 2020 at 23:16)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/lost.20in.20lifetimes/near/221245338">said</a>:</p>
<div class="codehilite"><pre><span></span><code>WARNING: bound `Clone` is not used in this function
</code></pre></div>
<div class="codehilite"><pre><span></span><code>That sounds awesome, though also potentially scary for some things -- like unsafe code that bounds on `Copy`.
</code></pre></div>



<a name="221259925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/lost%20in%20lifetimes/near/221259925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/lost.20in.20lifetimes.html#221259925">(Dec 31 2020 at 00:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/122651-general/topic/lost.20in.20lifetimes/near/221254844">said</a>:</p>
<blockquote>
<p>That sounds awesome, though also potentially scary for some things -- like unsafe code that bounds on <code>Copy</code>.</p>
</blockquote>
<p>Yeah, and would need to be smart around "regular" things like a method in a trait that doesn't need the bound but the trait itself requires it.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>